<%@page import="daointerfaces.DALException"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ page import="dto.OperatoerDTO" %>
<jsp:useBean id="func" class="Function.Functions" scope="session"/>
<html>
<HEAD>
<link rel="stylesheet" type="text/css" href="style.css">
<TITLE>Opret Bruger</TITLE>
</HEAD>
<body>
<div id="Centring">
<div class="sidenavn">Brugere</div>
<div id="Top"><div class="menu">
			<form method="POST">
				<table width=100%> 
					<tr>
						<td>
						<%= session.getAttribute("menu") %>
						</td>
						<td align="right">
						<% 
						OperatoerDTO tempOpr = (OperatoerDTO)session.getAttribute("loginID");
						%>
						<input type="submit" name="link" value="Edit Profile" class="hyperlinkButton" size=1>
						<input type="submit" name="link" value="log out" class="hyperlinkButton">
						</td>
					</tr>
				</table>
			</form>
</div></div>
<div id="Main">
	<br>
	<hr width=95% border=1>
	<br>
	<div class="Titlelille">	
	Opret Bruger
	</div>
	Indtast informationer og tryk Opret
	<div class="Indhold">
	
<form method="POST">
<%@ page import="java.util.List" %>
<%

if (request.getMethod().equals("POST") && request.getParameter("oprNavn") != null) {
	if(request.getParameter("action").equalsIgnoreCase("Opret")){
		try{
			int oprID = 0;
			String oprNavn;
			String oprCPR;
			String oprINI;
			String oprPassword;
			String oprRights;
			
			if(Integer.parseInt(request.getParameter("oprID")) > 0){
				oprID = Integer.parseInt(request.getParameter("oprID"));
			}else{
				throw new DALException("Invalid ID, ID need to be greater then 0");
			}
				
			if(request.getParameter("oprCpr") != null && !request.getParameter("oprCpr").trim().isEmpty()){
				oprCPR = request.getParameter("oprCpr");
			}else{
				throw new DALException("Invalid oprCpr");
			}
			
			if(request.getParameter("oprNavn") != null && !request.getParameter("oprNavn").trim().isEmpty()){
				oprNavn = request.getParameter("oprNavn").trim();
			}else{
				throw new DALException("Invalid name");
			}
			
			if(request.getParameter("oprNavn") != null && !request.getParameter("oprNavn").trim().isEmpty()){
				oprNavn = request.getParameter("oprNavn").trim();
			}else{
				throw new DALException("Invalid name");
			}
			
			oprINI = func.generateINI(request.getParameter("oprNavn"));
			
			if(request.getParameter("Password") != null && !request.getParameter("Password").isEmpty()){
				oprPassword = request.getParameter("Password");
			}else{
				throw new DALException("Invalid password");
			}
			
			oprRights = request.getParameter("rights");
			
	  		func.createOperatoer(new OperatoerDTO(oprID, oprNavn, oprINI , oprCPR, oprPassword, oprRights));
		}catch(DALException e){
			out.print( "<p style=\"color:red;\">"+e.getMessage()+"</p>. <br>");
		}catch(NumberFormatException e){
			out.print( "<p style=\"color:red;\">Invalid ID, ID need to be an integer</p>. <br>");
		}
	}
}
%>

<%	
out.print("<table id=\"tabledesign\" align=center>");
out.print("<tr>");
out.print("<th>ID</th>"); 
out.print("<th>Navn</th>");
out.print("<th>INI</th>"); 
out.print("<th>Cpr</th>");
out.print("<th>Password</th>");
out.print("<th>Rights</th>");
out.print("<th>Valg</th>");
out.print("</tr>");
	out.print("<tr>");
	out.print("<td>"+"<input type=\"text\" name=\"oprID\" value=\"\" size=1>"+"</td>"); 
	out.print("<td>"+"<input type=\"text\" name=\"oprNavn\" value=\"\" size=7>"+"</td>");
	out.print("<td>"+"Automatic"+"</td>");; 
	out.print("<td>"+"<input type=\"text\" name=\"oprCpr\" value=\"\" size=7>"+"</td>");
	out.print("<td>"+"<input type=\"text\" name=\"Password\" value=\"\"size=7>"+"</td>");
	out.print("<td>"+"<select name=\"rights\">");
		out.print("<option value=admin>admin</option>");
		out.print("<option value=farma>farma</option>");
		out.print("<option value=vaerk>vaerk</option>");
		out.print("<option value=opr selected>opr</option>");
	out.print("</select></td>");
	out.print("<td> <input type=\"submit\" name=\"action\" value=\"Opret\"> </td>");
	out.print("</tr>");
out.print("</table>");
%>
</form>
<br>
</div>
</div>
<div id="Bund">Brians Piller A/S 2013</div>
</div>
</body>
</html>